* Language W/O scale invariance
* Creating Estimate Points Data Set

clear 
edit
* Paste estimate point from excel
gen n=_n
gen m1=additive
*gen m2 =multiplicative
* Model transformations
local i =1
local mod= "additive"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=(`mod') if n==8 //level 2 Remove the 1.4

foreach num of numlist 9/17{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	//Remove the 1.4
}
foreach num of numlist 18/26 {
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 33/41{
replace m`i'=((1.4^`mod')/((1.4^`mod')+1))*4 if n==`num'	 	//by 4
}
saveold "Y:\Fuyao_Erlfang\estimates\pair\lang_level_add\point_estimates_add_pair_wo.dta", replace


clear
ed
* Paste from excel
gen n=_n
gen m2 =multiplicative
local i =2
local mod= "multiplicative"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=1+(`mod') if n==8 //level 3 //Remove the 1.4

foreach num of numlist 9/16{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	//Remove the 1.4
}
foreach num of numlist 17/25 32{
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 33/41{
replace m`i'=((1.1^`mod')/((1.1^`mod')+1))*6 if n==`num'	 	//by 6
}

saveold "Y:\Fuyao_Erlfang\estimates\pair\lang_level_mul\point_estimates_mul_pair_wo.dta", replace

 
 * Additive Model
	global path "Y:\Fuyao_Erlfang\estimates\pair\lang_level_add"
	use "$path\bootsresults.dta", clear 
	 
	* Creating the transformations:
	foreach num of numlist 2/4{
	gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
	}
	gen var5_2=(1-1.4^var5)

	gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

	gen var8_2=(var8) //Remove the 1.4
	foreach num of numlist 9/17{
	local j = `num'-1
	gen var`num'_2=var`j'_2+(var`num')	//Remove the 1.4	
	}
	foreach num of numlist 18/26 {
	gen var`num'_2=(1.4^var`num')		
	}
	foreach num of numlist 33/41{
	gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
	}
	cap drop q
	foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var26_2 var27-var32 var33_2-var41_2 var42-var53{
	sort `var'
	gen q=_n
	gen aux= `var' if q==25
	egen `var'_lb= max(aux) 
	drop aux
	gen aux= `var' if q==975
	egen `var'_ub= max(aux) 

	drop aux
	sum `var'
	gen aux= r(sd)
	egen `var'_sd= max(aux) 

	drop q aux
	}

	* TAKING OUT THE OUTLIERS FROM VARIANCE 
	foreach var of varlist var24_2 var25_2 {
	drop `var'_sd 
	sort  `var'
	gen q=_n
	sum `var'
	*sum `var' if `var'< r(max)
	sum `var' if q<995
	gen aux= r(sd)
	egen `var'_sd= max(aux) 
	drop aux q	
		
	}
	

	keep var*sd var*lb var*ub 
	gen q=_n
	foreach num of numlist 1 6 27/32 42/53{
		rename var`num'_lb var_lb_`num'
		rename var`num'_ub var_ub_`num'
		rename var`num'_sd var_sd_`num'
		
	}
	foreach num of numlist 2/5 7/26 33/41{
		rename var`num'_2_lb var_lb_`num'
		rename var`num'_2_ub var_ub_`num'
		rename var`num'_2_sd var_sd_`num'
		
	}
	keep if q<4
	reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
	sort numero q
	rename numero n
	merge m:1 n using "$path\point_estimates_add_pair_wo.dta"
	gen out= 1 if m1 > var_ub_ | m1 <var_lb_
br 
pause
drop out 
tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f) // 3 decimals
gen t = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t = "(" + var_sd_ + ")" if q==2
tostring m1 , replace force format(%6.3f) // 3 decimals
replace t=m1  if q ==1

keep q n var1 t
order q n var1 t
replace var1="" if q==2| q==3
saveold "$path\templ_add_pair_wo.dta" , replace


gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t 
drop q n


texsave using "$path\Lang_add_pair_wo.tex", width(\raggedleft) ///
    replace size(scriptsize) align(ll|c) location(ht) varlabels frag ///
    title(Language Additive) marker(tab:lang_)  ///
    footnote("Note:", size(scriptsize)) autonumber

/* 4 decimals
texsave using "$path\Lang_add_kbar_4dec.tex", width(\raggedleft) ///
    replace size(scriptsize) align(ll|c) location(ht) varlabels frag ///
    title(Language Additive) marker(tab:lang_)  ///
    footnote("Note:", size(scriptsize)) autonumber
*/

* Multiplicative
global path "Y:\Fuyao_Erlfang\estimates\pair\lang_level_mul"
use "$path/bootsresults.dta", clear
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) //Remove the 1.4
foreach num of numlist 9/16{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//Remove the 1.4	
}
foreach num of numlist 17/25 32{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 33/41{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*6		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var25_2 var26-var31 var32_2-var41_2 var42-var53{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}


keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 26/31 42/53{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/25 32/41{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/point_estimates_mul_pair_wo.dta"
gen out= 1 if m2 > var_ub_ | m2 <var_lb_
br 
pause
drop out 
tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t2 = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t2= "(" + var_sd_ + ")" if q==2
tostring  m2, replace force format(%6.3f)
replace t2=m2 if q ==1
keep q n var1 t2
order q n var1 t2
replace var1="" if q==2| q==3
saveold "$path/templ_multi_pair_wo.dta", replace 

gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t2

drop q n  
*paste manually the levels in a new column
texsave using "$path/Lang_Mult_pair_wo.tex", width(\raggedleft) ///
    replace size(scriptsize) align(llcllc) location(ht) varlabels frag ///
    title(Language Multiplicative) marker(tab:lang_)  ///
    footnote("Note:", size(scriptsize)) autonumber











